import matplotlib.pyplot as plt
import json
import matplotlib

matplotlib.rcParams['font.family'] = ['SimHei'] #黑体

x = []
y = []

with open('dianying.json', 'r', encoding='utf-8') as file:
    data = json.load(file)
    movie_list = data["movieList"]["list"]
    sorted_movies = sorted(movie_list, key=lambda m: m["showCount"], reverse=True)
    top_five_movies = sorted_movies[:5]

    for movie in top_five_movies:
        x.append(movie["movieInfo"]["movieName"])
        y.append(movie["showCount"])

# 创建图形和轴
# plt.plot(x, y) #plot是折线图  plt.scatter(x, y) # scatter是散点图 # plt.bar(x, y)  # bar是柱状图
plt.figure(figsize=(10, 6))  # 设置图形大小

colors = ['#FF9999', '#66B3FF', '#99FF99', '#FFCC99', '#FF99CC']
plt.bar(x, y, color=colors)
plt.xticks(rotation=45)

# 添加标题和标签
plt.title("电影排片场次排行榜(前五名)")
plt.xlabel("电影名称")
plt.ylabel("场次数量")
plt.tight_layout()

# 在柱子上方显示数值
for i, v in enumerate(y):
    plt.text(i, v + 0.5, str(v), ha='center')

# 显示图形
plt.show()